<?php

namespace app\index\controller;

use app\CommonController;
use app\index\middleware\Auth;
use app\index\Validate\MemberValidate;
use app\service\CoinAddressService;
use app\service\PayCoinService;
use think\exception\ValidateException;


class CoinAddress extends CommonController
{

    protected $middleware = [Auth::class];

    public function __construct(CoinAddressService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    public function index()
    {
        $where = [
            'member_id' => $this->request->uid,
            "status" => \app\model\CoinAddress::COIN_ADDRESS_STATUS_APPROVED
        ];
        $list = $this->service->getAll($where, "id,name,address", "is_default desc");
        $this->assign("list", $list);
        return $this->fetch("index");
    }

    public function addAddress()
    {
        if ($this->request->post()) {
            $data = $this->request->post();
            try {
                validate(MemberValidate::class)->scene("add-address")->check($data);
            } catch (ValidateException $e) {
                throw new ValidateException($e->getError());
            }
            $coinService = $this->app->make(PayCoinService::class);
            $res = $coinService->getOne(["id" => $data["coin_id"], "withdraw_status" =>1], "id");
            if (!$res) {
                $this->error("币种不存在，或者已经下架");
            }
            $where = [
                'member_id' => $this->request->uid,
                "coin_id" => $data["coin_id"]
            ];
            $model = $this->service->getOne($where, "id,name,address");
            if ($model && $model->status ==  \app\model\CoinAddress::COIN_ADDRESS_STATUS_REJECTED) {
                $this->error("该币种地址正在审核中");
            }
            if (!empty($model)) {
                $data["id"] = $model->id;
            };
            $res = $this->service->save([
                'coin_id' => $data["coin_id"],
                'name' => $data["name"],
                "address" => $data['address'],
                "member_id" => $this->request->uid
            ]);
            if ($res) {
                $this->success("添加成功");
            } else {
                $this->error("添加失败");
            }
        } else {
            $service = $this->app->make(PayCoinService::class);
            $list = $service->getAll(["withdraw_status" =>1], "id,coin_name", "sort asc");
            $this->assign("list", $list);
            $this->assign("info",null);
            return $this->fetch("add-address");
        }
    }

    public function delete()
    {
        $data = $this->request->post();
        try {
            validate(MemberValidate::class)->scene("address-delete")->check($data);
        } catch (ValidateException $e) {
            throw new ValidateException($e->getError());
        }
        $res = $this->service->get($data["id"], "id");
        if (empty($res)) {
            $this->error("地址不存在");
        }
        $ret = $res->delete();
        if ($ret) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

    public function setDefault()
    {
        $data = $this->request->post();
        try {
            validate(MemberValidate::class)->scene("address-set")->check($data);
        } catch (ValidateException $e) {
            throw new ValidateException($e->getError());
        }
        $res = $this->service->get($data["id"], ["id","is_default"]);
        if (empty($res)) {
            $this->error("地址不存在");
        }
        $this->service->update(["member_id"=>$this->request->uid],["is_default"=>0]);
        $res->is_default = 1;
        $ret = $res->save();
         if ($ret) {
             $this->success("设置成功");
         } else {
             $this->error("设置失败");
         }

    }

    public function edit()
    {
        $id = $this->request->param("id");
        if (!$id) {
            $this->error("请选择对应的地址id");
        }
        $res = $this->service->getOne(['member_id'=>$this->request->uid,'id'=>$id], "id,is_default,name,address,coin_id");
        if (empty($res)) {
            $this->error("不存在对应的地址");
        }
        $service = $this->app->make(PayCoinService::class);
        $list = $service->getAll(["withdraw_status" =>1], "id,coin_name", "sort asc");
        $this->assign("list", $list);
        $this->assign("info",$res);
        return $this->fetch("add-address");
    }
}